package com.soulkun.smarthome.ui.main.personal

import androidx.databinding.ObservableArrayList
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.soulkun.smarthome.R
import com.soulkun.smarthome.databinding.AppFragmentMainPersonalMessageFamilyBinding
import com.soulkun.smarthome.logic.dto.FamilyMessageDTO
import com.soulkun.smarthome.logic.enum.MessageCodeEnum
import com.soulkun.smarthome.logic.manager.BroadcastManager
import com.soulkun.smarthome.logic.vo.FamilyMessageHistoryItemVO
import com.soulkun.smarthome.logic.vo.FamilyMessageHistoryVO
import com.soulkun.smarthome.ui.main.personal.adapter.AppMainPersonalMessageFamilyContainerListAdapter
import com.soulkun.smarthome.ui.main.personal.viewmodel.AppMainPersonalMessageFamilyViewModel
import soulkun.library.helper.decoration.LinearSpaceItemDecorationHelper
import soulkun.library.mvvm.component.BaseDataBindingFragment
import soulkun.library.utils.time.DateUtils
import soulkun.library.utils.time.TimeUtils
import java.util.*

class AppMainPersonalMessageFamilyFragment :
    BaseDataBindingFragment<AppFragmentMainPersonalMessageFamilyBinding>(false) {

    private val viewModel by viewModels<AppMainPersonalMessageFamilyViewModel> { defaultViewModelProviderFactory }

    private val adapter by lazy {
        AppMainPersonalMessageFamilyContainerListAdapter(mContext, viewModel.familyMessageList)
    }

    private val familyBroadcastReceiver by lazy {
        BroadcastManager.SoulkunBroadcastReceiver(MessageCodeEnum.Family) {
            val familyMessageDTO = it as FamilyMessageDTO
            val messageDate = DateUtils.timestampToDate(familyMessageDTO.at).time
            viewModel.familyMessageList.find { familyMessageHistoryVO -> familyMessageHistoryVO.dateTime == messageDate }
                ?.let { alertMessageHistoryVO ->
                    alertMessageHistoryVO.messageList.add(
                        0, FamilyMessageHistoryItemVO(
                            familyMessageDTO.title,
                            if (familyMessageDTO.title != "删除家庭") {
                                String.format(
                                    familyMessageDTO.content,
                                    familyMessageDTO.activeUserName,
                                    familyMessageDTO.passiveUserName,
                                    familyMessageDTO.familyName
                                )
                            } else {
                                String.format(
                                    familyMessageDTO.content,
                                    familyMessageDTO.activeUserName,
                                    familyMessageDTO.familyName
                                )
                            },
                            TimeUtils.dateToStringWithFormat(
                                Date(familyMessageDTO.at.time),
                                TimeUtils.TIME_FORMAT
                            ) ?: ""
                        )
                    )
                    return@SoulkunBroadcastReceiver
                }
            viewModel.familyMessageList.add(
                0,
                FamilyMessageHistoryVO(
                    messageDate,
                    ObservableArrayList<FamilyMessageHistoryItemVO>().apply {
                        add(
                            FamilyMessageHistoryItemVO(
                                familyMessageDTO.title,
                                if (familyMessageDTO.title != "删除家庭") {
                                    String.format(
                                        familyMessageDTO.content,
                                        familyMessageDTO.activeUserName,
                                        familyMessageDTO.passiveUserName,
                                        familyMessageDTO.familyName
                                    )
                                } else {
                                    String.format(
                                        familyMessageDTO.content,
                                        familyMessageDTO.activeUserName,
                                        familyMessageDTO.familyName
                                    )
                                },
                                TimeUtils.dateToStringWithFormat(
                                    Date(familyMessageDTO.at.time),
                                    TimeUtils.TIME_FORMAT
                                ) ?: ""
                            )
                        )
                    })
            )
        }
    }

    override fun setLayout(): Int {
        return R.layout.app_fragment_main_personal_message_family
    }

    override fun initView() {
        requireDataBinding().rvMainPersonalMessageFamilyListContainer.run {
            layoutManager = LinearLayoutManager(mContext)
            adapter = this@AppMainPersonalMessageFamilyFragment.adapter
            if (itemDecorationCount == 0) {
                addItemDecoration(LinearSpaceItemDecorationHelper(10, 10, 15, 15, 10))
            }
        }
    }

    override fun initData() {
        viewModel.getFamilyMessageList()
    }

    override fun initListener() {
        BroadcastManager.registerBroadcast(
            mContext,
            MessageCodeEnum.Family,
            familyBroadcastReceiver
        )
    }

    override fun doOnCreate() {}

    override fun onDestroyView() {
        super.onDestroyView()
        BroadcastManager.unregisterBroadcast(mContext, familyBroadcastReceiver)
    }

}